//
//  TravelViewController.m
//  Fangmm
//
//  Created by ChaoZhou on 16/3/15.
//  Copyright © 2016年 com.fangmm. All rights reserved.
//

#import "TravelViewController.h"
#import "HttpServer.h"
#import "TravelTableViewCell.h"
#import "Schedule.h"
#import "VisitHouseMemberTableViewCell.h"
#import "VisitDateTableViewCell.h"
#import "EntrustfeeTableViewCell.h"
#import "IntentionfeeTableViewCell.h"
#import "PayTrusteesViewController.h"
#import "PayIntentionViewController.h"
#import <MJRefresh.h>
#import "PayTrustFeeViewController.h"
#import "House.h"
#import "MapViewViewController.h"
#import "CallServiceViewController.h"
#import <MobClick.h>

@interface TravelViewController ()<UITableViewDataSource,UITableViewDelegate,TravelTableViewCellDelegate,VisitDateTableViewCellDelegate>
{
    Schedule *schedule;
    BOOL      allPay;
    NSMutableArray *memberInfo;
}

@end

@implementation TravelViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self loadData];
    [MobClick beginLogPageView:@"行程页"];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"行程页"];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:240.0/255 green:240.0/255 blue:240.0/255 alpha:1];
    self.title = @"看房行程";
    UIBarButtonItem *leftBar = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"back-18-36"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(popToBack)];
    self.navigationItem.leftBarButtonItem = leftBar;
    [_mainTableView registerNib:[UINib nibWithNibName:@"TravelTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"TravelTableViewCell"];
    [_mainTableView registerNib:[UINib nibWithNibName:@"VisitHouseMemberTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"VisitHouseMemberTableViewCell"];
    [_mainTableView registerNib:[UINib nibWithNibName:@"VisitDateTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"VisitDateTableViewCell"];
    [_mainTableView registerNib:[UINib nibWithNibName:@"EntrustfeeTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"EntrustfeeTableViewCell"];
    [_mainTableView registerNib:[UINib nibWithNibName:@"IntentionfeeTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"IntentionfeeTableViewCell"];
}

#pragma mark - 加载数据
-(void)loadData
{
    [HttpServer getScheduleWithSuccessBlock:^(NSDictionary *responseData)
     {
         NSLog(@"小组行程1%@",responseData);
         schedule = [[Schedule alloc]initWithDic:responseData[@"schedule"]];
         [_mainTableView reloadData];
         UIImageView *defaultImage;
         UILabel *defaultLabel;
         if (schedule.scheduleArray.count == 0)
         {
             _mainTableView.hidden = YES;
             defaultImage.hidden = NO;
             defaultLabel.hidden = NO;
             if (!defaultImage)
             {
                 defaultImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"OX_roomie144-138"]];
             }
             if (!defaultLabel)
             {
                 defaultLabel = [[UILabel alloc]initWithFrame:CGRectMake(40, self.view.frame.size.height/2 + 10, self.view.frame.size.width - 80, 15)];
             }
             [defaultImage setFrame:CGRectMake(self.view.frame.size.width/2 - 36, self.view.frame.size.height/2 - 60, 72, 69)];
             [self.view addSubview:defaultImage];
             [self.view addSubview:defaultLabel];
             [defaultLabel setTextAlignment:NSTextAlignmentCenter];
             defaultLabel.font = [UIFont systemFontOfSize:11];
             defaultLabel.textColor = [UIColor colorWithRed:71.0/255 green:62.0/255 blue:58.0/255 alpha:1];
             defaultLabel.text = @"室友小组组团成功后行程信息会出现在这里";
         }
         if (schedule.scheduleArray.count > 0)
         {
             _mainTableView.hidden = NO;
             defaultImage.hidden = YES;
             defaultLabel.hidden = YES;
         }
         
     }
     withFailedBlock:^(id errorMsg)
     {
         NSLog(@"小组行程%@",errorMsg);
     }];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (schedule)
    {
        return schedule.scheduleArray.count + 1;
    }
    else
    {
        return 0;
    }
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (schedule)
    {
        if (section == 0)
        {
            //return 4;
            /*
             判断室友shizhang
             */
            if (schedule.leader == YES)
            {
                if ((schedule.trustee || schedule.earnest)== NO)
                {
                    return 4;
                }
                else if((schedule.trustee && schedule.earnest ) == YES)
                {
                    return 2;
                }
                else
                {
                    return 3;
                }
            }
            else
            {
                if (schedule.trustee == YES)
                {
                    return 2;
                }
                else
                {
                    return 3;
                }
            }
            
            
        }
        else
        {
            return 1;
        }
    }
    else
    {
        return 0;
    }
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        return 50;
    }
    else
    {
        return 260;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        switch (indexPath.row)
        {
            case 0:
            {
                VisitHouseMemberTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"VisitHouseMemberTableViewCell"];
                cell.memberArray = schedule.menberArray;
                cell.SuperViewController = self;
                [cell rectCell];
                return cell;
            }
                break;
            case 1:
            {
                VisitDateTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"VisitDateTableViewCell"];
                cell.delegate = self;
                cell.scheduleInfo = schedule;
                [cell rectCell];
                return cell;
            }
                break;
            case 2:
            {
                if (schedule.trustee == NO)
                {
                    EntrustfeeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EntrustfeeTableViewCell"];
                    cell.scheduleInfo = schedule;
                    [cell rectCell];
                    return cell;
                }
                else
                {
                    IntentionfeeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"IntentionfeeTableViewCell"];
                    cell.scheduleInfo = schedule;
                    [cell rectCell];
                    return cell;
                }
                
            }
                break;
            default:
            {
                IntentionfeeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"IntentionfeeTableViewCell"];
                cell.scheduleInfo = schedule;
                [cell rectCell];
                return cell;
            }
                break;
        }
    }
    else
    {
        TravelTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TravelTableViewCell"];
        cell.scheduleInfo = schedule;
        cell.callButton.tag = indexPath.section - 1;
        cell.delegate =self;
        cell.indexpath = indexPath;
        [cell configScheduleCellWithIndexpath:indexPath WithSchedule:schedule];
        return cell;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 && indexPath.row == 2)
    {
        if (schedule.trustee == NO)
        {
            PayTrustFeeViewController *payTrustFeeVC = [[PayTrustFeeViewController alloc]init];
            payTrustFeeVC.scheduleId = ((House*)[schedule.scheduleArray firstObject]).scheduleId;
            [self.navigationController pushViewController:payTrustFeeVC animated:YES];
        }
        else if(schedule.trustee == YES)
        {
            PayIntentionViewController *payIntentionVC = [[PayIntentionViewController alloc]init];
            payIntentionVC.scheduleId = ((House*)[schedule.scheduleArray firstObject]).scheduleId;
            payIntentionVC.scheduleInfo = schedule;
            [self.navigationController pushViewController:payIntentionVC animated:YES];
        }
        
    }
    if (indexPath.section == 0 && indexPath.row == 3)
    {
        if (schedule.leader == YES)
        {
#warning  支付意向金页面
            PayIntentionViewController *payIntentionVC = [[PayIntentionViewController alloc]init];
            payIntentionVC.scheduleId = ((House*)[schedule.scheduleArray firstObject]).scheduleId;
            payIntentionVC.scheduleInfo = schedule;
            [self.navigationController pushViewController:payIntentionVC animated:YES];
        }
    }
}
#pragma mark - poptoback
-(void)popToBack
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - configScheduleCell


#pragma mark - clickHeaderButtton
-(void)clickHeaderButton:(UIButton *)sender
{
    switch (sender.tag)
    {
        case 0:
        {
            
        }
            break;
        case 1:
        {
            
        }
            break;
        default:
        {
            
        }
            break;
    }
}

//-(void)parserData
//{
//    memberInfo = [NSMutableArray array];
//    for (HomePageUser *member in schedule.menberArray)
//    {
//        
//    }
//}

-(void)popToMapViewWithsender:(UIButton *)sender
{
    MapViewViewController *mapView = [[MapViewViewController alloc]init];
    mapView.communityName = ((House*)(schedule.scheduleArray[sender.tag])).houseCommunity;
    mapView.Latitude = ((House*)(schedule.scheduleArray[sender.tag])).meetingLatitude;
    mapView.Longitude = ((House*)(schedule.scheduleArray[sender.tag])).meetingLongitude;
    [self.navigationController pushViewController:mapView animated:YES];
}
-(void)popCallService
{
    CallServiceViewController *callServiceVC = [[CallServiceViewController alloc]init];
    [self presentViewController:callServiceVC animated:YES completion:nil];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
